package com.test.service.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.test.dao.Dao;
import com.test.service.Manager;
import com.test.util.ObjectUtils;
import com.test.util.StringUtils;

/**
 * Base class for Business Services - use this class for utility methods and
 * generic CRUD methods.
 * <p>
 * <a href="BaseManager.java.html"><i>View Source</i></a>
 * </p>
 * 
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
@SuppressWarnings("unchecked")
public class BaseManager implements Manager {

	protected Dao dao = null;

	protected final Log log = LogFactory.getLog(getClass());

	/**
	 * @see com.eport.ghostforest.service.Manager#getObject(java.lang.Class,
	 *      java.io.Serializable)
	 */
	public Object getObject(Class clazz, Serializable id) {
		return dao.getObject(clazz, id);
	}

	/**
	 * @see com.eport.ghostforest.service.Manager#getObjects(java.lang.Class)
	 */
	public List getObjects(Class clazz) {
		return dao.getObjects(clazz);
	}

	/**
	 * @see com.eport.ghostforest.service.Manager#removeObject(java.lang.Class,
	 *      java.io.Serializable)
	 */
	public void removeObject(Class clazz, Serializable id) {
		dao.removeObject(clazz, id);
	}

	/**
	 * @see com.eport.ghostforest.service.Manager#saveObject(java.lang.Object)
	 */
	public void saveObject(Object o) {
		dao.saveObject(o);
	}

	/**
	 * @see com.eport.ghostforest.service.Manager#setDao(com.eport.ghostforest.dao.Dao)
	 */
	public void setDao(Dao dao) {
		this.dao = dao;
	}

	/**
	 * 组合排序条件
	 * 
	 * @param list
	 * @return
	 */
	public String setOrder(List<String> list) {
		if (ObjectUtils.isNull(list)) {
			return StringUtils.EMPTY;
		}
		String[] orders = new String[list.size()];
		list.toArray(orders);
		StringBuffer sb = new StringBuffer(50);
		if (orders.length > 0) {
			sb.append(" order by ");
			for (String order : orders) {
				sb.append(order);
				sb.append(",");
			}
			sb.deleteCharAt(sb.length() - 1);
		}
		return sb.toString();
	}

	/**
	 * 组合查询条件
	 */
	public String setWhere(List<String> list) {
		if (ObjectUtils.isNull(list)) {
			return StringUtils.EMPTY;
		}
		String[] wheres = new String[list.size()];
		list.toArray(wheres);
		StringBuffer sb = new StringBuffer(50);
		for (String where : wheres) {
			sb.append(" and " + where);
		}
		return sb.toString();
	}
}
